package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.NoticeMsg;
import com.wanlian.entity.User;
import com.wanlian.error.ErrorCode;
import com.wanlian.error.SuccessCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.NoticeMsgServices;
import com.wanlian.service.UserServices;
import com.wanlian.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/noticemsg")
public class NoticeMsgController {
    @Autowired
    private NoticeMsgServices noticeMsgServices;

    @Autowired
    private UserServices userServices;

    /**
     * 发送指定用户消息/信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/send/one", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String sendMsgForOne(@RequestBody String data) {
        NoticeMsg noticeMsg = JsonStringUtil.readValue(data, NoticeMsg.class);
        ResultModel resultModel = noticeMsgServices.doSendMsgForOne(noticeMsg);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 群发消息/信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/send/many", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String sendMsgForMany(@RequestBody String data) {
        NoticeMsg noticeMsg = JsonStringUtil.readValue(data, NoticeMsg.class);
        ResultModel resultModel = noticeMsgServices.doSendMsgForMany(noticeMsg);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 删除消息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String doDel(@RequestBody String data) {
        NoticeMsg noticeMsg = JsonStringUtil.readValue(data, NoticeMsg.class);
        ResultModel resultModel = noticeMsgServices.doDeleteMsg(noticeMsg);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 分页查询
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/condition", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryMsg(@RequestBody String data) {
        PageCondition condition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = noticeMsgServices.queryNoticeMsgs(condition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 根据Id查询一条数据
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/query/one", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryMsgById(@RequestBody String data) {
        NoticeMsg noticeMsg = JsonStringUtil.readValue(data, NoticeMsg.class);
        ResultModel resultModel = noticeMsgServices.queryNoticeMsgById(noticeMsg.getId());
        return new JsonResult().Serailze(resultModel);

    }

    /**
     * 查看用户未查看的信息的条数
     *
     * @return
     */
    @RequestMapping(value = "/count/user", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryMsgByUserId(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = userSessionInfo.getLoginUser();
            resultModel = noticeMsgServices.queryCountByUserId(user.getId());
            resultModel.setBresult(true);
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查看该用户的所有信息（非系统公告）
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/user", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryMsgByUser(@RequestBody String data, HttpSession session) {
        Page page = JsonStringUtil.readValue(data, Page.class);
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = userSessionInfo.getLoginUser();
            resultModel = noticeMsgServices.queryMsgByUserId(user.getId(), page);
            resultModel.setBresult(true);
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        resultModel.setBresult(true);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 发送系统公告
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/send/system", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String sendSystemmsg(@RequestBody String data) {
        NoticeMsg noticeMsg = JsonStringUtil.readValue(data, NoticeMsg.class);
        ResultModel resultModel = noticeMsgServices.doSendSystemmsg(noticeMsg);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改信息已查看状态
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/set/haslook", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String doUpdateHasLook(@RequestBody String data) {
        NoticeMsg noticeMsg = JsonStringUtil.readValue(data, NoticeMsg.class);
        ResultModel resultModel = noticeMsgServices.doUpdateHasLook(noticeMsg.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询该用户的所有系统公告
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/system/user", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String querySystem(@RequestBody String data, HttpSession session) {
        Page page = JsonStringUtil.readValue(data, Page.class);
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);

        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = userSessionInfo.getLoginUser();
            resultModel = noticeMsgServices.querySystem(user.getId(), page);
            resultModel.setBresult(true);
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询用户未查看的公告条数
     *
     * @return
     */
    @RequestMapping(value = "/system/count", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryCountSystem(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);

        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = userSessionInfo.getLoginUser();
            resultModel = noticeMsgServices.queryCountSystem(user.getId());
            resultModel.setBresult(true);
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询所有系统公告
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/system/all", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryAllSystem(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = noticeMsgServices.queryAllSystem(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * APP端全部标记为已读
     *
     * @return
     */
    @RequestMapping(value = "/update/all", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String updateAllNoticeMsg(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = userSessionInfo.getLoginUser();
            List<NoticeMsg> noticeMsgs = noticeMsgServices.queryAllByUserId(user.getId());
            for (NoticeMsg noticeMsg : noticeMsgs) {
                noticeMsgServices.doUpdateHasLook(noticeMsg.getId());
            }
            resultModel.setBresult(true);
            resultModel.setMessage(SuccessCode.NOTICEMSG_READALL);
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        return new JsonResult().Serailze(resultModel);
    }



    /**
     * 修改消息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String doUpdate(@RequestBody String data) {
        NoticeMsg noticeMsg = JsonStringUtil.readValue(data, NoticeMsg.class);
        ResultModel resultModel = noticeMsgServices.doUpdateMsg(noticeMsg);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * APP端获取用户最新一条的系统公告
     *
     * @return
     */
    @RequestMapping(value = "/system/new", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryNewSystemByUserId(HttpSession session) {
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        ResultModel resultModel = new ResultModel();
        if (userSessionInfo != null && userSessionInfo.getLoginUser() != null) {
            User user = userSessionInfo.getLoginUser();
            NoticeMsg noticeMsg = noticeMsgServices.queryNewSystemByUserId(user.getId());
            if (noticeMsg != null) {
                resultModel.setBresult(true);
                resultModel.setObject(noticeMsg);

            } else {
                resultModel.setBresult(false);
                resultModel.setMessage(SuccessCode.NOTCE_SYSTEM_DEF);
            }
        } else {
            resultModel.setBresult(false);
            resultModel.setErrorTag(ErrorCode.ERROR_SESSION_TAG);
            resultModel.setMessage(ErrorCode.LOGIN_USER);
        }
        return new JsonResult().Serailze(resultModel);
    }
}
